package com.learn.project2.controller.wx.order;

import com.learn.project2.bean.MarketAdmin;
import com.learn.project2.bean.MarketOrderGoods;
import com.learn.project2.bean.MarketUser;
import com.learn.project2.model.bo.wx.order.OrderCommentBO2;
import com.learn.project2.model.bo.wx.order.OrderSubmitBO;
import com.learn.project2.model.vo.wx.order.OrderDetailVO2;
import com.learn.project2.model.vo.wx.order.OrderListVO2;
import com.learn.project2.service.wx.order.OrderService2;
import com.learn.project2.utils.Result;
import org.apache.catalina.User;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.PostMapping;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author zhizhuke
 * @version 1.0
 * @description:
 * @date 2022-01-08 15-57
 */
@RestController
@RequestMapping("wx/order")
public class OrderController2 {

    @Autowired
    OrderService2 orderService2;


    /**
     * @description:
     * @param:
     * @return:
     * @author zhizhuke
     * @date: 2022-01-10 22:40
     */
    @RequestMapping("confirm")
    public Result confirm(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        orderService2.confirm(orderId);
        return Result.ok();
    }

    /**
     * @description:
     * @param:
     * @return:
     * @author zhizhuke
     * @date: 2022-01-10 22:40
     */
    @RequestMapping("refund")
    public Result refund(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        orderService2.refund(orderId);
        return Result.ok();
    }

    /** 
     * @description: 
     * @param:
     * @return:  
     * @author zhizhuke
     * @date: 2022-01-12 09:19
     */ 
    @RequestMapping("submit")
    public Result submit(@RequestBody OrderSubmitBO orderSubmitBO) {

        orderService2.submit(orderSubmitBO);
        return Result.ok();
    }


    /**
     * @description:
     * @param:
     * @return:
     * @author zhizhuke
     * @date: 2022-01-10 22:40
     */
    @RequestMapping("delete")
    public Result delete(Integer orderId) {
        orderService2.delete(orderId);
        return Result.ok();
    }

    /**
     * @description:
     * @param:
     * @return:
     * @author zhizhuke
     * @date: 2022-01-10 22:40
     */
    @RequestMapping("goods")
    public Result goods(Integer orderId, Integer goodsId) {

        MarketOrderGoods marketOrderGoods = orderService2.goods(orderId, goodsId);
        return Result.ok(marketOrderGoods);
    }
    @RequestMapping("prepay")
    public Result prepay(@RequestBody Map map) {
        Integer orderId = Integer.parseInt(((String) map.get("orderId"))) ;

        orderService2.prepay(orderId);
        return Result.ok();
    }




    @PostMapping("comment")
    /**
     * @description: 订单评论
     * @author wangzhuo
     * @date 2022-01-10 16:42
     * @version 1.0
     */
    public Result comment(@RequestBody OrderCommentBO2 orderCommentBO2) {
        int i = orderService2.setComment(orderCommentBO2);
        if (i == 200) {
            return Result.ok();
        }
        return Result.fail("评论失败");
    }

    @RequestMapping("list")
    /**
     * @description:
     * @author wangzhuo
     * @date 2022-01-10 19:38
     * @version 1.0
     */
    public Result list(Integer showType,Integer page,Integer limit) {
        //获取当前登录用户,用MarketUser接受
        MarketUser marketUser = (MarketUser) SecurityUtils.getSubject().getPrincipal();
         String username=marketUser.getUsername();
        OrderListVO2 orderService2List = orderService2.getList(showType, page, limit, username);
        return Result.ok(orderService2List);
    }

    @RequestMapping("detail")
    /**
     * @description:
     * @author wangzhuo
     * @date 2022-01-10 19:39
     * @version 1.0
     */
    public Result detail(Integer orderId) {
        OrderDetailVO2 orderDetailVO2 = orderService2.getDetail(orderId);
        return Result.ok(orderDetailVO2);
    }

    @PostMapping("cancel")
    /**
     * @description:
     * @author wangzhuo
     * @date 2022-01-10 19:39
     * @version 1.0
     */
    public Result cancel(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        orderService2.cancel(orderId);
        return Result.ok("取消订单成功");
    }
}
